package org.fall.system.controller.backstage;

import org.fall.system.model.dto.AdminUserDTO;
import org.fall.system.model.ResultEntity;
import org.fall.system.service.BackstageIndexService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * 后台管理系统-index界面
 *
 * @author zjh
 * @since 2021/3/19 15:56
 */
@Controller
@RequestMapping("/admin/index/")
public class BackstageIndexController {

    @Resource
    private BackstageIndexService backstageIndexService;

    /**
     * 登录操作
     * @param dto dto
     * @param session session
     * @return 登录结果
     */
    @PostMapping("login")
    @ResponseBody
    public ResultEntity login(@RequestBody AdminUserDTO dto, HttpSession session) {
        return backstageIndexService.login(dto, session);
    }

    /**
     * 退出登录
     * @param session session
     * @return 退出登录页面
     */
    @GetMapping("logout")
    public String logout(HttpSession session) {
        // 注销
        session.invalidate();
        return "login";
    }

    /**
     * 注册操作
     * @param dto dto
     * @return 注册结果
     */
    @PostMapping("register")
    @ResponseBody
    public ResultEntity register(@RequestBody AdminUserDTO dto) {
        if (!Objects.equals(dto.getPassword(), dto.getConfirmPassword())) {
            return ResultEntity.failed("请确认输入的密码是否有误！");
        }
        return backstageIndexService.register(dto);
    }
}
